package com.system.cacheClient.client.impl;

import com.system.cacheClient.client.ZSetCacheClient;
import com.system.cacheClient.common.CacheR;
import com.system.cacheClient.feign.ZSetCacheFeignClient;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.Set;

/**
 * ZSet缓存客户端实现类
 */
@Component
@RequiredArgsConstructor
public class ZSetCacheClientImpl implements ZSetCacheClient {
    
    /**
     * Feign客户端，用于与服务端通信
     */
    private final ZSetCacheFeignClient zSetCacheFeignClient;
    
    @Override
    public <T> CacheR<Boolean> zadd(String key, T value, double score) {
        return zSetCacheFeignClient.zadd(key, score, value);
    }
    
    @Override
    public <T> CacheR<Long> zrem(String key, T... values) {
        return zSetCacheFeignClient.zrem(key, values);
    }
    
    @Override
    public <T> CacheR<Double> zscore(String key, T member) {
        // 简化实现，实际应该通过服务端接口获取分数
        return CacheR.fail("未实现");
    }
    
    @Override
    public <T> CacheR<Long> zrank(String key, T member) {
        // 简化实现，实际应该通过服务端接口获取排名
        return CacheR.fail("未实现");
    }
    
    @Override
    public CacheR<Set<String>> zrange(String key, long start, long end) {
        CacheR<Set<String>> result = zSetCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            // 简化实现，实际应该根据分数排序并获取指定范围
            return CacheR.success(result.getData(), "获取有序集合范围成功");
        }
        return CacheR.fail("获取有序集合范围失败");
    }
    
    @Override
    public CacheR<Long> zcard(String key) {
        CacheR<Set<String>> result = zSetCacheFeignClient.get(key);
        if (result.getCode() == 200 && result.getData() != null) {
            long size = result.getData().size();
            return CacheR.success(size, "获取有序集合大小成功");
        }
        return CacheR.fail(0L, "获取有序集合大小失败");
    }
    
    @Override
    public CacheR<Long> zcount(String key, double min, double max) {
        // 简化实现，实际应该通过服务端接口获取指定分数范围的元素个数
        return CacheR.fail(0L, "未实现");
    }
}